import { mapGetters } from 'vuex'
import { cloneDeep } from 'lodash'
export default {
  data() {
    return {}
  },
  computed: mapGetters(['homeColumnData']),
  methods: {
    //点击文章跳转
    articleJump(row, id, target) {
      if (row.newsType == 2) {
        window.open(row.linkAddr)
        return
      }
      let article = row.id
      if (target == '_blank') {
        const news = this.$router.resolve({
          name: 'article',
          query: { id, article },
        })
        window.open(news.href, '_blank')
      } else {
        this.$router.push({
          name: 'article',
          query: { id, article },
        })
      }
    },
    //点击栏目|更多跳转
    columnMoreJump(id) {
      this.$router.push({
        name: 'article',
        query: { id },
      })
    },
    //获取栏目信息
    async getColumnInfo(id) {
      let memu = cloneDeep(this.homeColumnData)
      let filterData = this.flat(memu).filter((v) => v.id == id)
      return filterData.length > 0 ? filterData[0] : {}
    },
    flat(arr) {
      return [].concat(
        ...arr.map((item) => [].concat(item, ...this.flat(item.childList))),
      )
    },
    //通过栏目id获取文章列表
    async getListByColumnId(obj) {
      let listQuery = {
        page: obj.page || 1,
        limit: obj.limit || 10,
        status: 1,
        auditStatus: 2,
        columnId: obj.columnId || '',
      }
      let memu = cloneDeep(this.homeColumnData)
      listQuery.columnId = this.flatObj(memu)
        .filter((item) => item.pid == listQuery.columnId)
        .filter((item) => item.classType == '11')
        .map((item) => item.id)
        .toString()
      let res = await this.$store.dispatch(
        'queryOpenApiNewsContents',
        listQuery,
      )
      return res
    },
    //通过书籍id获取文章
    async getListByJouralId(obj) {
      let listQuery = {
        page: obj.page || 1,
        limit: obj.limit || 10,
        status: 1,
        auditStatus: 2,
        journalId: obj.journalId || '',
      }
      let res = await this.$store.dispatch(
        'queryOpenApiNewsContents',
        listQuery,
      )
      return res
    },
  },
}
